<?php

namespace app\tour\controller;

use Think\Db;
use think\Request;
use app\tour\model\Tour as TourModel;
use think\Session;

vendor('qiniu/php-sdk/autoload.php');

use Qiniu\Auth;
use Qiniu\Storage\UploadManager; //实例化上传类

class Tour extends Common
{

    public function info(){

        //获取景区信息
        $field = ['tour_name','tour_subject','tour_tel','type','should_konwn','tour_tag','tour_covers','tour_time','address','longitude','latitude','city'];

        $tour = TourModel::getTourInfo($field);

        $this->assign('tour',$tour);

        return view();

    }

    public function editTourInfo(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $post = $post['data'];
            //$start = explode('-',$post['tour_time']);
            $eidtData = [

                'tour_name'    => $post['tour_name'],
                'tour_subject' =>$post['tour_subject'],
                'tour_tel'     => $post['tour_tel'],
                'type'         => $post['type'],
                'tour_tag'     => $post['tour_tag'],
                'tour_time'    =>$post['tour_time'],
                'address'      => $post['address'],
                'city'    => $post['province'].$post['city'].$post['district'],
            ];

            $res = TourModel::editTourInfo($eidtData);

            if ($res !== false){
                $id = Session::get('tour_id');
                $add = $post['province'].$post['city'].$post['district'].$post['address'];
                //添加经纬度

                $data = [
                    'longitude'=>getDoc($add)[0],
                    'latitude'=>getDoc($add)[1]
                ];
                Db::name('tours')->where('tour_id',$id)->update($data);
                return ['code'=>0];

            }else{

                return ['code'=>1];

            }

        }

    }

    /**
     * 景区封面图上传
     */
    public function uploadCoverImg()
    {
        $file = request()->file('file');
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
        // 上传到七牛后保存的文件名
        $key = 'lv/tour/' . substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = config("qiniu")["accessKey"];
        $secretKey = config("qiniu")["secretKey"];

        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        $bucket = config("qiniu")["bucket"];
        //域名
        $domain = config("qiniu")["domain"];
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return ["code" => 0, "msg" => $err, "data" => ""];
        } else {
            //返回图片的完整URL
            $imgPath = $domain . '/' . $key;

            TourModel::editTourInfo(['tour_covers'=>$imgPath]);

            return ["code" => 1, "msg" => "上传成功", "data" => $imgPath];
        }
    }

}